const express = require('express');
const loginMiddleware = require("../middleware/LoginMiddleware");

const CommentDao = require('../database/CommentDao')

const router = express.Router();

router.post('/', loginMiddleware, async (req, res) => {
    let Comments = req.body;
    try{//addComment(noticeID, userId, content,fID)
        let result = await CommentDao.addComment(Comments.notice_id, Comments.user_id, Comments.comment_content, Comments.comment_fID);
        console.log("comments修改的结果/是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({code:2000,msg:"添加成功",data:null});
    }catch(err){
        console.log(err);
        res.send({code:5000,msg:"添加失败",data:null});
    }
});

router.delete("/noticeId/:noticeId",loginMiddleware,async (req,res)=>{
    let noticeId = req.params.noticeId;
    try{
        let result = await CommentDao.deleteCommentsByNoticeId(noticeId);
        console.log("comments删除的结果/noticeId/:noticeId是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({code:2000,msg:"删除成功",data:null});
    }catch(err){
        console.log(err);
        res.send({code:5000,msg:"删除失败",data:null});
    }
})

router.delete('/:id', loginMiddleware, async (req, res) => {
    let id = req.params.id;
    try{
        let result = await CommentDao.deleteComment(id);
        console.log("comments删除的结果/:id是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({code:2000,msg:"删除成功",data:null});
    }catch(err){
        console.log(err);
        res.send({code:5000,msg:"删除失败",data:null});
    }
})

router.get('/:noticeId', loginMiddleware, async (req, res) => {
    let noticeId = req.params.noticeId;
    try{
        let result = await CommentDao.selectCommentsByNoticeId(noticeId);
        console.log("comments查询的结果/:noticeId是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({code:2000,msg:"查询成功",data:result});
    }catch(err){
        console.log(err);
        res.send({code:5000,msg:"查询失败",data:null});
    }
})

module.exports = router;